package com.aaa.mrcai.controller;


import com.aaa.mrcai.annotion.AAALog;
import com.aaa.mrcai.pojo.entity.Emp;
import com.aaa.mrcai.pojo.vo.EmpVo;
import com.aaa.mrcai.query.EmpQuery;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.service.EmpService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @author meowyf
 * @version 1.0
 * @Name Z_MJ
 * @date 2021/12/02 19:37:10
 */
@RestController
@RequestMapping("aaa")
public class EmpController {
    @Autowired
    private EmpService empService;

    @RequestMapping("uploadEmpExcel")
    public AjaxResult uploadDeptExcel(MultipartFile e) throws Exception {

        /*读取所有要导入的部门数据*/
        ExcelReaderBuilder read = EasyExcel.read(e.getInputStream());
        /*将数据转成Map形式*/
        List<Map<String, Object>> objects = read.doReadAllSync();

        /*调用service,将数据存到数据库中*/

        EmpVo empVo = new EmpVo();
        for (Map<String, Object> m : objects) {
            empVo.setEname((String) m.get(0));
            empVo.setHiredate((String) m.get(1));
            empVo.setTelephone((String) m.get(2));
            empVo.setDid(Integer.parseInt((String) m.get(3)));
            empVo.setRid(Integer.parseInt((String) m.get(4)));
            empVo.setAge(Integer.parseInt((String) m.get(5)));
            AjaxResult ajaxResult = empService.AddEmp(empVo);
            if (ajaxResult.getCode() == 2){
                return AjaxResult.defeated("导入中断，请检查信息是否有误");
            }
        }
        return AjaxResult.success("导入成功");
    }


    @RequestMapping("download")
    public void downloadInfo(EmpQuery query, HttpServletResponse resp) throws Exception{
        //调用service，查询符合条件的学生
        AjaxResult list = empService.list(query);
        Map<String,Object> data = (Map<String, Object>) list.getData();
        List<Emp> pageData= (List<Emp>) data.get("empListAll");

        // 2 下载excel
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        resp.setContentType("application/vnd.ms-excel");
        resp.setCharacterEncoding("utf-8");

        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("员工信息", "UTF-8");
        resp.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");


        EasyExcel.write(   resp.getOutputStream()  , Emp.class   )     .sheet("员工信息").doWrite( pageData    );


    }

    @RequestMapping("list")
    public AjaxResult List(EmpQuery empQuery){

        return empService.list(empQuery);
    }

    @RequestMapping("AddEmp")
    @AAALog("添加员工")
    public AjaxResult AddEmp(EmpVo empVo){

        return empService.AddEmp(empVo);
    }

    @RequestMapping("/getEmp/{id}")
    public AjaxResult getEmp(@PathVariable("id") int id){
        return empService.getEmp(id);
    }

    @RequestMapping("updateEmp")
    @AAALog("修改员工")
    public AjaxResult UpdateEmp(EmpVo empVo){
        return empService.UpdateEmp(empVo);
    }

    @RequestMapping("/deleteEmp/{id}")
    @AAALog("删除部门")
    public AjaxResult deleteEmp(@PathVariable("id") int id){
        return empService.deleteEmp(id);
    }

    @RequestMapping("listDept")
    public AjaxResult listDept(){

        return empService.listDept();
    }

    @RequestMapping("listRole")
    public AjaxResult listrole(){
        return empService.listrole();
    }

}
